package com.ruoyi.service.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.service.domain.ServicePackages;
import com.ruoyi.service.entity.packages.PCPackagesDetailReq;
import com.ruoyi.service.entity.packages.PCPackagesListVO;
import com.ruoyi.service.entity.packages.PCSuppliesVO;
import com.ruoyi.service.service.IServicePackagesService;
import com.ruoyi.service.service.impl.PcPackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 耗材包Controller
 * 
 * @author ZDL
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/service/packages")
public class ServicePackagesController extends BaseController
{
    @Autowired
    private IServicePackagesService servicePackagesService;

    @Autowired
    private PcPackagesService pcPackagesService;

    /**
     * 查询耗材包列表
     */
    @PreAuthorize("@ss.hasPermi('service:packages:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServicePackages servicePackages)
    {
        startPage();
        List<ServicePackages> list = servicePackagesService.selectServicePackagesList(servicePackages);
        return getDataTable(list);
    }

    /**
     * 查询耗材包列表
     */
    @PreAuthorize("@ss.hasPermi('service:packages:list')")
    @GetMapping("/listNew")
    public TableDataInfo listNew(ServicePackages servicePackages)
    {
        startPage();
        List<PCPackagesListVO> list = pcPackagesService.getPackagesList(servicePackages);
        return getDataTable(list);
    }

    /**
     * 保存耗材包详情
     */
    @PreAuthorize("@ss.hasPermi('service:packages:list')")
    @PostMapping("/savePackages")
    public AjaxResult savePackages(@RequestBody PCPackagesDetailReq packageReq)
    {
        try {
            Integer result = pcPackagesService.savePackages(packageReq);
            if (result == 0){
                return AjaxResult.success("保存成功");
            }else {
                return AjaxResult.error("保存失败");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询耗材包列表
     */
    @PreAuthorize("@ss.hasPermi('service:packages:list')")
    @GetMapping("/getSupplies")
    public AjaxResult getSupplies(ServicePackages servicePackages)
    {
        try {

            List<PCSuppliesVO> list = pcPackagesService.getSupplies();
            return AjaxResult.success(list);

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 导出耗材包列表
     */
    @PreAuthorize("@ss.hasPermi('service:packages:export')")
    @Log(title = "耗材包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServicePackages servicePackages)
    {
        List<ServicePackages> list = servicePackagesService.selectServicePackagesList(servicePackages);
        ExcelUtil<ServicePackages> util = new ExcelUtil<ServicePackages>(ServicePackages.class);
        util.exportExcel(response, list, "耗材包数据");
    }

    /**
     * 获取耗材包详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:packages:query')")
    @GetMapping(value = "/{packagesId}")
    public AjaxResult getInfo(@PathVariable("packagesId") Long packagesId)
    {
        return success(servicePackagesService.selectServicePackagesByPackagesId(packagesId));
    }
    /**
     * 获取耗材包详细信息New
     */
    @PreAuthorize("@ss.hasPermi('service:packages:query')")
    @GetMapping(value = "/new/{packagesId}")
    public AjaxResult getPackages(@PathVariable("packagesId") Long packagesId)
    {
        return success(pcPackagesService.getPackages(packagesId));
    }

    /**
     * 新增耗材包
     */
    @PreAuthorize("@ss.hasPermi('service:packages:add')")
    @Log(title = "耗材包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServicePackages servicePackages)
    {
        //servicePackages.setCreateBy(getUsername());
        //servicePackages.setUpdateBy(getUsername());
        //...
        return toAjax(servicePackagesService.insertServicePackages(servicePackages));
    }

    /**
     * 修改耗材包
     */
    @PreAuthorize("@ss.hasPermi('service:packages:edit')")
    @Log(title = "耗材包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServicePackages servicePackages)
    {
        return toAjax(servicePackagesService.updateServicePackages(servicePackages));
    }

    /**
     * 删除耗材包
     */
    @PreAuthorize("@ss.hasPermi('service:packages:remove')")
    @Log(title = "耗材包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{packagesIds}")
    public AjaxResult remove(@PathVariable Long[] packagesIds)
    {
        return toAjax(servicePackagesService.deleteServicePackagesByPackagesIds(packagesIds));
    }

    /**
     * 查询耗材明细
     */
    @PreAuthorize("@ss.hasPermi('service:packages:supplies')")
    @GetMapping("/{packagesIds}")
    public AjaxResult selectSupplies(@PathVariable("packagesId") Long packagesId)
    {
        return success(servicePackagesService.selectServiceSuppliesListByPackagesId(packagesId));
    }
}
